<?php

// 本类由系统自动生成，仅供测试用途
class LoginAction extends CommonAction {

    public function index() {


        $uid = ToolsAction::checkLogin();


        if ($uid) {

            $this->error('您已经处于登录状态！请退出登录后再试！');
        }








        $this->display();
    }

    public function register() {



        $uid = ToolsAction::checkLogin();


        if ($uid) {

            $this->error('您已经处于登录状态！请退出登录后再试！');
        }











//inviteid
        $inviteid = $this->_get('inviteid');


        if (isset($inviteid)) {


            $inviteid_nickname = ToolsAction::getUserinfo($inviteid, 'member_nickname');

            if ($inviteid_nickname == '') {

                $this->error('方便您的朋友认出您来，请完善昵称后再邀请！', __APP__ . '/Member/');
            }

            $this->assign('inviteid_nickname', $inviteid_nickname);


            $this->assign('inviteid', $inviteid);
        }



        $this->display();
    }

    public function findPassword() {





        $this->display();
    }

    public function findPassword2() {




        $email = $this->_post('email');



        if (!ToolsAction::IS_email($email)) {


            $this->error('请填写正确的email', __APP__ . '/Login/');
        }


        $memberDB = M('member_basic');


        $where['member_email'] = $email;
        $where['member_accountstatus'] = 1;




        $info = $memberDB->where($where)->find();


        if (!$info) {
            $this->error('此邮箱没有注册！', __APP__ . '/Login/');
        }




        if (!cookie('findpassword_sn')) {



            $sn = ToolsAction::orderSN();



            session('findpassword_sn', $sn);
            session('findpassword_email', $email);



            ToolsAction::SendMail($email, 'www.IT123.org提醒：找回密码验证【若不是本人操作，不用理会！】', '验证码是：' . $sn);


            cookie('findpassword_sn', md5($email), 60 * 60 * 24);
        }










        $this->display();
    }

    public function findPassword3() {

        $email_yzm = $this->_post('email_yzm');


        if (session('findpassword_sn') != $email_yzm) {

            $this->error('邮箱验证码错误！', __APP__ . '/Login/');
        }



        $this->assign('email_yzm', $email_yzm);



        $this->display();
    }

    public function findWordPressDO() {


        if (!session('findpassword_sn')) {
            $this->error('非法操作！');
        }


        $email_yzm = $this->_post('email_yzm');






        if (!isset($email_yzm)) {

            $this->error('非法操作！');
        }


        if (session('findpassword_sn') != $email_yzm) {

            $this->error('非法操作！');
        }




        $password = $this->_post('password');

        $password1 = $this->_post('password1');



        if ($password != $password1) {

            $this->error('非法操作！');
        }

        if (!session('findpassword_email')) {


            $this->error('非法操作');
        }

        $email = session('findpassword_email');



        $memberDB = M('member_basic');

        $where['member_email'] = $email;


        $info = $memberDB->where($where)->find();

        if (!$info) {

            $this->error('非法操作！');
        }


        $password = ToolsAction::encrypt($password);


        $data['member_id'] = $info['member_id'];

        $data['member_email'] = session('findpassword_email');

        $data['member_password'] = $password;

        if ($memberDB->save($data) !== FALSE) {

            $this->success('密码修改成功！请登录', __URL__);
        } else {

            $this->error('密码修改失败，请重试！请登录', __URL__ . '/findPassword/');
        }
    }

    public function verifyEmail() {

        $this->display();
    }

    public function ajaxVerifyEmailDo() {


        $verify_string = $this->_post('verify_string');



        if (!session('member.member_id')) {
            $this->error('请登录');
        }



        if (session('member.member_uidcode') != $verify_string) {


            die(json_encode(array('status' => FALSE)));
        } else {


            $mDB = M('member_basic');

            $data['member_accountstatus'] = 1;
            $data['member_id'] = session('member.member_id');




            $mDB->save($data);

            $where['member_id'] = session('member.member_id');

            $member = $mDB->where($where)->find();


            session('member', $member);

//邀请用户发放积分


            $invite_id = ToolsAction::getInviteInfo($data['member_id']);

            if ($invite_id) {

//更新邀请的状态
                ToolsAction::UpInviteStatic($data['member_id']);


//给积分
                ToolsAction::rewardPoints(3, 3, 0, $invite_id, $member['member_id'], '邀请用户' . $member['member_email'] . '获得3个积分');

                $invite_email = ToolsAction::getUserinfo($invite_id, 'member_email');

//提醒邮件
                ToolsAction::SendMail($invite_email, 'www.it123org提示：恭喜！您推荐的用户已经注册并验证，您将获得本站的3个积分奖励', date('Y-m-d:H-m-s') . 'www.it123org提示：恭喜！您推荐的用户已经注册并验证，您将获得本站的3个积分奖励,感谢您对本站的支持！');
            }







            die(json_encode(array('status' => TRUE)));
        }
    }

    public function loginDo() {



        $useremail = trim($this->_post('user_email'));



        $user_pwd = trim($this->_post('user_pwd'));


        if (!isset($useremail) || !isset($user_pwd)) {
            $this->error('请填写相关选项');

            die();
        }


        if (!ToolsAction::IS_email($useremail)) {
            $this->error('请填写正确的邮箱!');
            die();
        }



        $userDB = M('member_basic');

        $where['member_email'] = $useremail;


        $member = $userDB->where($where)->find();






        if (!$member) {
            $this->error('本邮箱并未注册!');
        }




        if ($member['member_password'] != ToolsAction::encrypt($user_pwd)) {

            $this->error('密码错误！');
        }










//0未激活1正常2被锁
        if ($member['member_is_cancel'] == 1) {
            $this->error('您的账户已被锁定，请联系我们');
            die();
        }


        $member['member_face32'] = ToolsAction::getUserFace($member['member_id'], '32');
        
        

        session('member', $member);

      //  P(session('member'));


        ToolsAction::addLoginRecord($member['member_id']);





        if (cookie('LURL')) {

            $this->success('登录成功!', cookie('LURL'));
            die();
        } else {

            $this->success('登录成功!', '__APP__/Member/');
            die();
        }
    }

    public function regDo() {



        $useremail = trim($this->_post('email'));
        $user_pwd = trim($this->_post('password'));
        $user_pwd1 = trim($this->_post('password1'));




        $inviteid = $this->_post('inviteid');

  







        if (!isset($useremail) || !isset($user_pwd) || !isset($user_pwd1)) {
            $this->error('请填写相关选项');
        }


        if ($user_pwd != $user_pwd1) {
            $this->error('确认密码不一致！');
        }

        if (!ToolsAction::IS_email($useremail)) {
            $this->error('请填写正确的邮箱!');
        }


        $userDB = M('member_basic');

        $where['member_email'] = $useremail;


        $user = $userDB->where($where)->find();



        if ($user) {
            $this->error('本邮箱已经注册，请登录或者是找回密码');
        }




        $data_user['member_regtime'] = time();
        $data_user['member_email'] = $useremail;
        $data_user['member_password'] = ToolsAction::encrypt($user_pwd);



        $data_user['member_uidcode'] = 123456;







        $uid = $userDB->add($data_user);

        if (!$uid) {
            $this->error('注册失败,请重试');
        }








        $this->success('注册成功！', '__URL__/');

        die();
    }

    public function sendaActivationEmail() {


        $uid = cookie(md5('ActivationUser'));

        if (!$uid) {

            $this->error('请重新登录！');
        }


        $mDB = M('member_basic');


        $where['member_id'] = $uid;



        $info = $mDB->where($where)->find();






        if (!$info) {

            $this->error('请重新注册认证超时！');
        }


        if ($info['member_accountstatus'] > 0) {

            $this->error('已经认证，请直接登录', '__URL__/');
            die();
        }








        if ($info['member_accountstatus'] == 0) {


            if (!cookie(md5('ActivationSendEmail'))) {

                $str_title = '[www.it123.org]注册会员邮件激活验证';
                $str_contont = '尊敬用户：请点击链接激活账号：http://www.it123.org/Login/regCctivation/u/' . $uid . '/p/' . $info['member_uidcode'];

                ToolsAction::SendMail($info['member_email'], $str_title, $str_contont);

                cookie(md5('ActivationSendEmail'), 1, 60);

                $this->success('激活链接已经发送您邮箱，请登录邮箱查看', '__URL__/verifyEmail/');

                die();
            }




            $this->error('1分钟后再试！', '__APP__/Login/verifyEmail/', '__URL__/verifyEmail/');


            die();
        }


        $this->success('激活链接已经发送您邮箱，请到邮箱查看，', '__URL__/verifyEmail/');

        die();
    }

    public function regCctivation() {


        $uid = $this->_get('u');


        $p = $this->_get('p');


        if (!is_numeric($uid)) {
            $this->error('参数错误');
        }



        $mDB = M('member_basic');


        $where['member_id'] = $uid;


        $info = $mDB->where($where)->find();

        if (!$info) {
            $this->error('认证超时，请重新注册，谢谢！');
        }



        if ($info['member_accountstatus'] == 1) {


            $this->error('您的账号已经完成验证，无需再试！');
        }






//0未激活1正常2被锁
        if ($info['member_accountstatus'] == 2) {
            $this->error('您的账户已被锁定，请联系我们');
        }






        if ($info['member_uidcode'] != $p) {

            $this->error('验证失败！');
        }



        $data['member_accountstatus'] = 1;
        $data['member_id'] = $uid;




        $mDB->save($data);

        $where['member_id'] = $uid;

        $member = $mDB->where($where)->find();










        ToolsAction::writeAutoLoginCookie($member['member_id']);







        session('member', $member);





        ToolsAction::addLoginRecord($member['member_id']);



        $str_title2 = date('Y-m-d') . '[www.it123.org]网站新用户：' . $member['member_email'] . '完成注册并激活了账号！';



        $str_contont2 = date('Y-m-d:H:m:s') . '新会员：' . $member['member_email'] . '完成注册并激活了账号，赶快去审核吧！加油哦！';

        ToolsAction::SendMail('1261639911@qq.com', $str_title2, $str_contont2);



        if (cookie('LURL')) {

            $this->success('您的账号已经激活！感谢您对本站的支持！', cookie('LURL'));
        } else {
            $this->success('您的账号已经激活！感谢您对本站的支持！', '__APP__/Member/');
        }
    }

    public function qq() {


        $qc = new QC();
        $qc->qq_login();
    }

    public function logout() {

        $jumpUrl = cookie('jumpUrl');



//卸载SESSION
        session_unset();
        session_destroy();


        session('member', null);

        cookie(ToolsAction::encrypt('Uid_CODE'), 1);
        cookie(ToolsAction::encrypt('UID'), NULL);


//删除用于自动登录的COOKIE
//@setcookie('auto', '', time() - 3600, '/');


        if ($jumpUrl) {

            $this->success('安全退出', $jumpUrl);
        } else {


            $this->success('安全退出', '__APP__/Index/');
        }
    }

}
